package com.example.template;

import com.example.service.SysRabbitMQService;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author chentao
 * @version 1.0
 * @description: TODO
 * @date 2025/3/18 下午9:43
 */
@Service
public class SysRabbitMQTemplate {

    Logger logger = LoggerFactory.getLogger(getClass());

    @Resource
    private SysRabbitMQService sysRabbitMQService;

    /***
     * 发送mq消息
     * @param exchange
     * @param routingKey
     * @param properties
     * @param body
     * @throws Exception
     */
    public void publish(String exchange, String routingKey, AMQP.BasicProperties properties, byte[] body) throws Exception {
        Channel channel = null;
        try {
            channel = sysRabbitMQService.getConnection().createChannel();
            channel.basicPublish(exchange, routingKey, properties, body);
        } finally {
            if (channel != null) {
                channel.close();
            }
        }
    }
}
